package com.bosssoft.springcloud.controller;

import com.bosssoft.springcloud.entity.dto.PermissionDTO;
import com.bosssoft.springcloud.service.PermissionService;
import com.bosssoft.springcloud.entity.vo.CommenResult;
import com.bosssoft.springcloud.entity.vo.PermissionVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    /***
     * 获取权限信息
     * @param permissionVO
     * @return
     */
    @GetMapping("/rbac/permission/getPermission")
    public CommenResult getPermission(@RequestBody PermissionVO permissionVO){
        PermissionDTO permissionDTO = new PermissionDTO();
        BeanUtils.copyProperties(permissionDTO,permissionVO);
        PermissionDTO permission = permissionService.findPermissionById(permissionDTO.getId());
        if (permission != null){
            return new CommenResult(200,"查询成功",permission.toString());
        }
        return new CommenResult(404,"查询失败",null);
    }

    /***
     * 添加权限
     * @param permissionVO
     * @return
     */
    @GetMapping("/rbac/permission/addPermission")
    public CommenResult addPermission(@RequestBody PermissionVO permissionVO){
        PermissionDTO permissionDTO = new PermissionDTO();
        BeanUtils.copyProperties(permissionDTO,permissionVO);
        int res = permissionService.addPermission(permissionDTO.getId());
        if (res > 0){
            return new CommenResult(200,"添加成功",res);
        }
        return new CommenResult(404,"添加失败",res);
    }

    /***
     * 移除权限
     * @param permissionVO
     * @return
     */
    @GetMapping("/rbac/permission/removePermission")
    public CommenResult removePermission(@RequestBody PermissionVO permissionVO){
        PermissionDTO permissionDTO = new PermissionDTO();
        BeanUtils.copyProperties(permissionDTO,permissionVO);
        int res = permissionService.removePermission(permissionDTO.getId());
        if (res > 0){
            return new CommenResult(200,"移除成功",res);
        }
        return new CommenResult(404,"移除失败",res);
    }
}
